package org.rjxh.examfront.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.rjxh.examfront.pojo.*;
import org.rjxh.examfront.mapper.ExamQuestionMapper;
import org.rjxh.examfront.service.IExamQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.rjxh.examfront.web.tools.JwtUtils;
import org.rjxh.examfront.web.tools.RedisCache;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 考试考题 服务实现类
 * </p>
 *
 * @author BA7LGJ
 * @since 2024-04-04
 */
@Service
public class ExamQuestionServiceImpl extends ServiceImpl<ExamQuestionMapper, ExamQuestion> implements IExamQuestionService {
    @Resource
    private RedisCache redisCache;

    @Resource
    private JwtUtils jwtUtils;
    @Resource
    private ExamQuestionMapper examQuestionMapper;

    @Override
    public R getQuestions(Page<ExamQuestion> page) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = loginUser.getUser().getUserId();
        Long recordId = redisCache.getCacheObject("examRecordingUser:" + userId);
        //todo 获得examRecording
        ExamRecord examRecord = redisCache.getCacheObject("examRecording:"+recordId);
        if (Objects.isNull(examRecord)) {
            return new R(400, "考试记录不存在");
        }

        Long examId = examRecord.getExamExamination().getExamId();

        IPage<ExamQuestion> examQuestionIPage = examQuestionMapper.selectQuestionsByExamId(page, examId);

        return new R(200, "获取成功", examQuestionIPage);
    }
}
